<?php
/**
 * ECSHOP JS调用
 * ============================================================================
 * 版权所有 2005-2010 上海商派网络科技有限公司，并保留所有权利。
 * 网站地址: http://www.ecshop.com；
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和
 * 使用；不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * $Author: chigao $
 * $Id: javascript.php 17063 2010-03-25 06:35:46Z chigao $
*/
define('IN_ECS', true);

require(dirname(__FILE__) . '/includes/init.php');

if ((DEBUG_MODE & 2) != 2)
{
    $smarty->caching = true;
}

$act = isset($_REQUEST['act']) ? $_REQUEST['act'] : "" ;
if($act=="")
{
	exit();
}
$limit  = isset($_REQUEST['limit']) ? intval($_REQUEST['limit']) : 1 ;

/* 页面的缓存ID */
$cache_id = sprintf('%X', crc32($act . '-' . $limit));

if (!$smarty->is_cached('category.dwt', $cache_id))
{
	$list='';
	if($act === "best")
	{
		$list=get_new_category_recommend_goods('best',$limit);
	}
	elseif($act === "new")
	{
		$list=get_new_category_recommend_goods('new',$limit);
	}
	elseif($act === "hot")
	{
		$list=get_new_category_recommend_goods('hot',$limit);
	}
	elseif($act ==="promote")
	{
		$list=get_new_category_recommend_goods('promote',$limit);
	}
	elseif($act === "news")
	{
	 	$list = get_new_cat_articles("4,11,12,13",$limit);
	}
	$smarty->assign("list",$list);
}

$shopurl = $ecs->url();
$smarty->assign("shopurl",$shopurl);

$smarty->display("javascript/".$act.".dwt",$cache_id);

//函数
function get_new_category_recommend_goods($type = '', $limit = 3)
{


    $sql =  'SELECT g.goods_id, g.goods_name, g.goods_name_style, g.market_price, g.shop_price AS org_price, g.promote_price, ' .
                "IFNULL(mp.user_price, g.shop_price * '$_SESSION[discount]') AS shop_price, ".
                'promote_start_date, promote_end_date, g.goods_brief, g.goods_thumb, goods_img, b.brand_name ' .
            'FROM ' . $GLOBALS['ecs']->table('goods') . ' AS g ' .
            'LEFT JOIN ' . $GLOBALS['ecs']->table('brand') . ' AS b ON b.brand_id = g.brand_id ' .
            "LEFT JOIN " . $GLOBALS['ecs']->table('member_price') . " AS mp ".
                    "ON mp.goods_id = g.goods_id AND mp.user_rank = '$_SESSION[user_rank]' ".
            'WHERE g.is_on_sale = 1 AND g.is_alone_sale = 1 AND g.is_delete = 0 ';
    $num = 0;
	
    $type2lib = array('best'=>'recommend_best', 'new'=>'recommend_new', 'hot'=>'recommend_hot', 'promote'=>'recommend_promotion');
	
    $num = $limit;

    switch ($type)
    {
        case 'best':
            $sql .= ' AND is_best = 1';
            break;
        case 'new':
            $sql .= ' AND is_new = 1';
            break;
        case 'hot':
            $sql .= ' AND is_hot = 1';
            break;
        case 'promote':
            $time = gmtime();
            $sql .= " AND is_promote = 1 AND promote_start_date <= '$time' AND promote_end_date >= '$time'";
            break;
    }


    $order_type = $GLOBALS['_CFG']['recommend_order'];
    $sql .= ($order_type == 0) ? ' ORDER BY g.sort_order, g.last_update DESC' : ' ORDER BY RAND()';
    $res = $GLOBALS['db']->selectLimit($sql, $num);

    $idx = 0;
    $goods = array();
    while ($row = $GLOBALS['db']->fetchRow($res))
    {
        if ($row['promote_price'] > 0)
        {
            $promote_price = bargain_price($row['promote_price'], $row['promote_start_date'], $row['promote_end_date']);
            $goods[$idx]['promote_price'] = $promote_price > 0 ? price_format($promote_price) : '';
        }
        else
        {
            $goods[$idx]['promote_price'] = '';
        }

        $goods[$idx]['id']           = $row['goods_id'];
        $goods[$idx]['name']         = $row['goods_name'];
        $goods[$idx]['brief']        = $row['goods_brief'];
        $goods[$idx]['brand_name']   = $row['brand_name'];
        $goods[$idx]['short_name']   = $GLOBALS['_CFG']['goods_name_length'] > 0 ?
                                       sub_str($row['goods_name'], $GLOBALS['_CFG']['goods_name_length']) : $row['goods_name'];
        $goods[$idx]['market_price'] = price_format($row['market_price']);
        $goods[$idx]['shop_price']   = price_format($row['shop_price']);
        $goods[$idx]['thumb']        = get_image_path($row['goods_id'], $row['goods_thumb'], true);
        $goods[$idx]['goods_img']    = get_image_path($row['goods_id'], $row['goods_img']);
        $goods[$idx]['url']          = build_uri('goods', array('gid' => $row['goods_id']), $row['goods_name']);

        $goods[$idx]['short_style_name'] = add_style($goods[$idx]['short_name'], $row['goods_name_style']);
        $idx++;
    }

    return $goods;
}

function get_new_cat_articles($cat,$limit = 5)
{
	$sql = 'SELECT article_id, title, author, add_time, art_logo , file_url, open_type' .
               ' FROM ' .$GLOBALS['ecs']->table('article') .
               ' WHERE is_open = 1 AND  cat_id in ('.$cat.')'.
               ' ORDER BY article_type DESC, article_id DESC limit '.$limit;
	$res = $GLOBALS['db']->getAll($sql);
	
	$list= array();
	
	foreach($res as $idx => $row)
	{
		$list[$row['article_id']]['id'] = $row['article_id'];
		$list[$row['article_id']]['title'] = $row['title'];
		$list[$row['article_id']]['author'] = $row['author'];
		$list[$row['article_id']]['url']         = $row['open_type'] != 1 ? build_uri('article', array('aid'=>$row['article_id']), $row['title']) : trim($row['file_url']);
		$list[$row['article_id']]['art_logo']   = $row['art_logo'] ;
        $list[$row['article_id']]['add_time']    = date($GLOBALS['_CFG']['date_format'], $row['add_time']);
	}
	return $list;
}
?>